﻿// --------------------------------------------------------------------------------------------------------------------
// <summary>
//   The edit.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Widgets.DisqusCommentsSummary
{
    using System;
    using System.Web;

    using App_Code.Controls;
    using BlogEngine.Core;

    /// <summary>
    /// The edit.
    /// </summary>
    public partial class Edit : WidgetEditBase
    {
        #region Public Methods

        /// <summary>
        /// Saves this the basic widget settings such as the Title.
        /// </summary>
        public override void Save()
        {
            var settings = this.GetSettings();
            settings["numberofitems"] = this.txtNumberOfItems.Text;
            settings["widgettype"] = this.cboWidgetType.SelectedValue;
            settings["showmoderatorsinranking"] = this.cboShowModerators.SelectedValue;
            settings["combotheme"] = this.cboColorTheme.SelectedValue;
            settings["combotabview"] = this.cboDefaultTabView.SelectedValue;
            settings["commentexcerptlength"] = this.txtCommentExcerptLength.Text;
            settings["showavatars"] = this.cboShowAvatars.SelectedValue;
            settings["avatarsize"] = this.cboAvatarSize.SelectedValue;

            this.SaveSettings(settings);
            Blog.CurrentInstance.Cache.Remove("widget_disquscommentssummary");
        }

        #endregion

        #region Methods

        /// <summary>
        /// Raises the <see cref="E:System.Web.UI.Control.PreRender"/> event.
        /// </summary>
        /// <param name="e">
        /// An <see cref="T:System.EventArgs"/> object that contains the event data.
        /// </param>
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (this.Page.IsPostBack)
            {
                return;
            }

            var settings = this.GetSettings();

            this.txtNumberOfItems.Text = settings.ContainsKey("numberofitems") ? settings["numberofitems"] : "5";
            this.cboWidgetType.SelectedValue = settings.ContainsKey("widgettype") ? settings["widgettype"] : "combo";
            this.cboShowModerators.SelectedValue = settings.ContainsKey("showmoderatorsinranking") ? settings["showmoderatorsinranking"] : "true";
            this.cboColorTheme.SelectedValue = settings.ContainsKey("combotheme") ? settings["combotheme"] : "blue";
            this.cboDefaultTabView.SelectedValue = settings.ContainsKey("combotabview") ? settings["combotabview"] : "people";
            this.txtCommentExcerptLength.Text = settings.ContainsKey("commentexcerptlength") ? settings["commentexcerptlength"] : "200";
            this.cboShowAvatars.SelectedValue = settings.ContainsKey("showavatars") ? settings["showavatars"] : "true";
            this.cboAvatarSize.SelectedValue = settings.ContainsKey("avatarsize") ? settings["avatarsize"] : "32";
        }

        #endregion
    }
}